package com.wxtf.platform.main.keyserobj;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wxtf.platform.interfaces.CommonsCallProvider;
import com.wxtf.platform.plugins.user.UserBean;
import com.wxtf.platform.supers.LogicBase;
import com.wxtf.platform.tools.ConstantInfoHelper;
import com.wxtf.platform.tools.AccessDataBaseUtil;
import com.wxtf.platform.tools.FormatUtil;
import com.wxtf.platform.tools.XLSUtils;
import java.io.File;
import java.util.List;
import com.wxtf.platform.tools.Common;
/**
 * <li>Title: KeySet.java</li>
 * <li>Project: baseplatform</li>
 * <li>Package: com.wxtf.platform.main.zdfwdx</li>
 * <li>Description: 重点服务对象信息维护</li>
 * <li>Copyright: Copyright (c) 2012</li>
 * <li>Company: wxthtf Technologies </li>
 * <li>Created on Aug 27, 2012 10:29:02 AM</li>
 * 
 * @author wangzhi
 * @version 1.0.0.0
 * 
 */
public class KeySet extends LogicBase implements CommonsCallProvider{
	private static final Log log = LogFactory.getLog(KeySet.class);

	public KeySet(String jsonArray){

	}
	
	@Override
	public Object run() {
		Object msg = "";
		try {
			if("queryPage".equals(this.getMethodName())) {
				msg = this.select(this.getLogicData());
				return msg;
			} else if("update".equals(this.getMethodName())) {
				msg = this.update(this.getLogicData());
			} else if("expData".equals(this.getMethodName())) {
				msg = this.expData(this.getLogicData());
			} else {
				msg = "\"没有【" + this.getMethodName() + "】该方法\"";
			}

			if(msg == null || "[null]".equals(msg)) {
				msg = "\"\"";
			}
			if(msg.toString().startsWith("{") || msg.toString().startsWith("[")){
				return msg;
			}
			msg = ConstantInfoHelper.generateJSONString(msg.toString());
		} catch(Exception e) {
			msg = ConstantInfoHelper.generateErrorJSONString();
			log.error(e, e);
		} finally {
			this.clear();
		}
		return msg.toString();
	}

	String[] fields = {
			"tbe_id","org_id","tbe_idcard","tbe_work_permit",         //3
			"tbe_employment_forms","tbe_name","tbe_former_name","tbe_spell_code","tbe_sex","tbe_age","tbe_birthday","tbe_nation","tbe_native_palce",    //12
			"tbe_birthplace","tbe_join_time","tbe_seniority","tbe_family_phone","tbe_mobile","tbe_address","tbe_marital_status","tbe_marital_condition",   //20
			"tbe_married_time","tbe_marriage_certificate","tbe_political_status","tbe_qualification","tbe_graduate_college","tbe_major","tbe_work_area",   //27
			"tbe_position","tbe_is_again_birth","tbe_is_barrenness","tbe_is_late_marriage","tbe_is_late_birth","tbe_status","tbe_ba_status",    //34
			"tbe_birth_control_measures","tbe_es_time","tbe_is_es","tbe_es_reason","tbe_children_status","tbe_order","tbe_memo",      //41
			"reserve1","reserve2","reserve3","reserve4","reserve5","create_by","create_time","last_modify_by","last_modify_time","tbfi_children_num"    //50
	};
	@Override
	public Object select(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		sql = "from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id  where t1.tbe_status not in ('102-3','102-4','102-5') and t1.tbe_employment_forms <> '31' ";
		JSONArray array = JSONArray.fromObject(jsonArray);
		//查询type：init/serch1/serch2
		if(array.size() > 0) {
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			String types = jsonObject.getString("types");
			if("init".equals(types)) {// 初始查询	
				sql = "from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id  where t1.tbe_status not in ('102-3','102-4','102-5') and t1.tbe_employment_forms <> '31'  and t1.org_id in (" + user.getCurSubOrgId() + ") ";				
			} else if("serch1".equals(types)) {// 条件查询
				String orgIds = jsonObject.getString("orgIds");//机构ID串
				String tbe_is_es = jsonObject.getString("tbe_is_es");//是否重点
				String tbe_es_reason = jsonObject.getString("tbe_es_reason");//重点理由
				String tbe_name = jsonObject.getString("tbe_name");//姓名
				String tbe_birth_control_measures = jsonObject.getString("tbe_birth_control_measures");//节育措施
				String tbe_idcard = jsonObject.getString("tbe_idcard");//身份证
				StringBuffer buff = new StringBuffer();
				if(StringUtils.isBlank(orgIds)){
					buff.append(" and t1.org_id in (").append(user.getCurSubOrgId()).append(")");
				} else {
					buff.append(" and t1.org_id in (").append(user.getCurSubOrgIds(orgIds)).append(")");
				}
				if(StringUtils.isNotBlank(tbe_is_es)){
					if(!"null".equals(tbe_is_es)){	
					buff.append(" and t1.tbe_is_es = '").append(tbe_is_es).append("'");
					}
				}
				if(StringUtils.isNotBlank(tbe_es_reason)){
					if(!"null".equals(tbe_es_reason)){
					buff.append(" and t1.tbe_es_reason = '").append(tbe_es_reason).append("'");
					}
				}
				if(StringUtils.isNotBlank(tbe_name)){
					buff.append(" and t1.tbe_name like '%").append(tbe_name).append("%'");
				}
				if(StringUtils.isNotBlank(tbe_birth_control_measures)){
					if(!"null".equals(tbe_birth_control_measures)){
					buff.append(" and t1.tbe_birth_control_measures = '").append(tbe_birth_control_measures).append("'");
					}
				}
				if(StringUtils.isNotBlank(tbe_idcard)){
					buff.append(" and t1.tbe_idcard like '%").append(tbe_idcard).append("%'");
				}
				sql += buff.toString();
			}
		}
		// 排序
		String sortname = this.getRequest().getParameter("sortname");
		String sortorder = this.getRequest().getParameter("sortorder");
		if(sortname!=null&&sortname!=null) {
			sql += " order by " + sortname + " " + sortorder;
		}
		String countSql = "select count(*) " + sql;
		String querySql = "select t1.tbe_id,t1.org_id,t1.tbe_idcard,t1.tbe_work_permit,t1.tbe_employment_forms,t1.tbe_name,t1.tbe_former_name,t1.tbe_spell_code,t1.tbe_sex,t1.tbe_age,t1.tbe_birthday,t1.tbe_nation,t1.tbe_native_palce,t1.tbe_birthplace,t1.tbe_join_time,t1.tbe_seniority,t1.tbe_family_phone,t1.tbe_mobile,t1.tbe_address,t1.tbe_marital_status,t1.tbe_marital_condition,t1.tbe_married_time,t1.tbe_marriage_certificate,t1.tbe_political_status,t1.tbe_qualification,t1.tbe_graduate_college,t1.tbe_major,t1.tbe_work_area,t1.tbe_position,t1.tbe_is_again_birth,t1.tbe_is_barrenness,t1.tbe_is_late_marriage,t1.tbe_is_late_birth,t1.tbe_status,t1.tbe_ba_status,t1.tbe_birth_control_measures,t1.tbe_es_time,t1.tbe_is_es,t1.tbe_es_reason,t1.tbe_children_status,t1.tbe_order,t1.tbe_memo,t1.reserve1,t1.reserve2,t1.reserve3,t1.reserve4,t1.reserve5,t1.create_by,t1.create_time,t1.last_modify_by,t1.last_modify_time,t2.tbfi_children_num " + sql;
		return AccessDataBaseUtil.query4SQLPagging(fields, countSql, querySql, this.getPage(), this.getPagesize(), null);
	}

	@Override
	public Object insert(Object jsonArray) throws Exception {
		return null;
	}

	
	
	@Override
	public Object update(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		String curTime = FormatUtil.getCurentDateTime();
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String tbe_id = jsonObject.getString("tbe_id");//主键ID
		String tbe_is_es = jsonObject.getString("tbe_is_es");//是否设为重点服务对象
		String tbe_es_reason = jsonObject.getString("tbe_es_reason");//
		String tbe_es_time = jsonObject.getString("tbe_es_time");//
		StringBuffer buff = new StringBuffer();
		buff.append("update tb_employee set tbe_is_es = '").append(tbe_is_es).append("',tbe_es_reason='").append(tbe_es_reason).append("',tbe_es_time='").append(tbe_es_time).append("',last_modify_by ='").append(user.getUserId()).append("',last_modify_time = '").append(curTime).append("' where tbe_id = '").append(tbe_id).append("'");
		String sql = buff.toString();
		String[] sqls = { sql };
		AccessDataBaseUtil.executeSQLUpdate(sqls);
		return "\"设置成功\"";
	}
	
	
	
	/** 
	 * 描述：重点对象导出
	 */
	public Object expData(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String orgIds = jsonObject.getString("orgIds");//机构ID串
		String tbe_name = jsonObject.getString("tbe_name");//姓名
		String tbe_idcard = jsonObject.getString("tbe_idcard");//身份证
		String tbe_is_es = jsonObject.getString("tbe_is_es");
		String tbe_es_reason = jsonObject.getString("tbe_es_reason");
		String tbe_birth_control_measures = jsonObject.getString("tbe_birth_control_measures");
		StringBuffer buff = new StringBuffer();
		if("mysql".equals(user.getDbType())){
			buff.append(" select if(t1.tbe_is_es='0','否','是'),") ; 
		}else if("oracle".equals(user.getDbType())) {
			buff.append(" select decode(t1.tbe_is_es='0','否','是'),") ; 
		}
		buff.append(" t1.tbe_name,t1.tbe_idcard,t1.tbe_sex,t1.tbe_es_time,t1.tbe_birthday," ) 
			.append(" (select tmgd_name from TM_GENERAL_DICTIONARY where tmgd_id =t1.tbe_nation),")
			.append(" (select tmgd_name from TM_GENERAL_DICTIONARY where tmgd_id =t1.tbe_marital_status)," )
			.append(" t2.tbfi_children_num," )
			.append(" if(t1.tbe_is_again_birth='0','否','是')," )
			.append(" if(t1.tbe_is_barrenness='0','否','是')," )
			.append(" (select tmgd_name from TM_GENERAL_DICTIONARY where tmgd_id =t1.tbe_birth_control_measures)," )
			.append(" (select tmgd_name from TM_GENERAL_DICTIONARY where tmgd_id =t1.tbe_es_reason)," )
			.append(" t1.tbe_memo")
			.append(" from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id  where t1.tbe_status not in ('102-3','102-4','102-5') and t1.tbe_employment_forms <> '31'  ");

		if(StringUtils.isBlank(orgIds)){
			buff.append(" and t1.org_id in (").append(user.getCurSubOrgId()).append(")");
		} else {
			buff.append(" and t1.org_id in (").append(user.getCurSubOrgIds(orgIds)).append(")");
		}
		if(StringUtils.isNotBlank(tbe_name)) {
			buff.append(" and t1.tbe_name like '%").append(tbe_name).append("%'");
		}
		if(StringUtils.isNotBlank(tbe_idcard)) {
			buff.append(" and t1.tbe_idcard like '%").append(tbe_idcard).append("%'");
		}		
		if(StringUtils.isNotBlank(tbe_is_es)) {
			if(!"null".equals(tbe_is_es)){
			buff.append(" and t1.tbe_is_es = '").append(tbe_is_es).append("'");
			}
		}
		if(StringUtils.isNotBlank(tbe_es_reason)) {
			if(!"null".equals(tbe_es_reason)){
				buff.append(" and t1.tbe_es_reason = '").append(tbe_es_reason).append("'");
			}
		}
		if(StringUtils.isNotBlank(tbe_birth_control_measures)) {
			if(!"null".equals(tbe_birth_control_measures)){
				buff.append(" and t1.tbe_birth_control_measures = '").append(tbe_birth_control_measures).append("'");
			}
		}
		
		List<Object[]> list = AccessDataBaseUtil.query(buff.toString(), null);
		
		String url = this.getRequest().getSession().getServletContext().getRealPath("/jsp/app/xls/");
		String model = url + "/model/keySet.xls";
		String file = url + "/file/keySet.xls";
		String rfile = "jsp/app/xls/file/keySet.xls";
		File ffrom = new File(model);
		File fto = new File(file);
		Common.copyFile(ffrom, fto);
		XLSUtils.writeXls(list, fto, "重点服务对象信息服务", 2);//写xls内容
		return "\"" + rfile + "\"";
	}
	

	
	
	
	@Override
	public Object delete(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object recordCount(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object setRequest(HttpServletRequest request) throws Exception {
		this.request = request;
		return request;
	}

	@Override
	public Object updateBatch(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object onCreate(Object jsonArray) {
		return null;
	}

	@Override
	public Object onDestory(Object jsonArray) {
		return null;
	}

	@Override
	public Object onInit(Object jsonArray) {
		return null;
	}
}


